<?php
/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
App::uses('Controller', 'Controller');

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package		app.Controller
 * @link		http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
 */
class AppController extends Controller {
    public $components = array('DebugKit.Toolbar','Session');

    /**
	 * prints out an array
	 */
	function pa($arr) {
		echo '<pre>';
		print_r($arr);
		echo '</pre>';
	}


	/*
	 *	add comma to every fields
	*/
	function convert_to_values($data) {
		$num = count($data);
		$result = '';
		for ($c=0; $c < $num; $c++) {

			if($c != 0)
				$result = $result.','.$data[$c];
			else
				$result = $data[$c];
 
		}

		return $result;
	}

	
	/**
	 * uploads files to the server
	 * @params:
	 *		$folder 	= the folder to upload the files e.g. 'img/files'
	 *		$formdata 	= the array containing the form files
	 *		$itemId 	= id of the item (optional) will create a new sub folder
	 * @return:
	 *		will return an array with the success of each file upload
	 */
	function uploadFiles($folder, $file, $itemId = null) {
	$folder_url = WWW_ROOT.$folder;
	$rel_url = $folder;
	
	// create the folder if it does not exist
	if(!is_dir($folder_url)) {
		mkdir($folder_url);
	}

	// if itemId is set create an item folder
	if($itemId) {
		// set new absolute folder
		$folder_url = WWW_ROOT.$folder.'/'.$itemId; 
		// set new relative folder
		$rel_url = $folder.'/'.$itemId;
		// create directory
		if(!is_dir($folder_url)) {
			mkdir($folder_url);
		}
	}

	$permitted = 'application/vnd.ms-excel';

	$typeOK = false ; 

	if($permitted == $file['type']) {
		$typeOK = true;
	}

	$filename = str_replace(' ', '_', $file['name']);


	if($typeOK) {
			// switch based on error code
		switch($file['error']) {
			case 0:
					// check filename already exists
			if(!file_exists($folder_url.'/'.$filename)) {
						// create full filename
				$full_url = $folder_url.'/'.$filename;
				$url = $rel_url.'/'.$filename;
						// upload the file
				$success = move_uploaded_file($file['tmp_name'], $url);
			} else {
						// create unique filename and upload file
				ini_set('date.timezone', 'Europe/London');
				$now = date('Y-m-d-His');
				$full_url = $folder_url.'/'.$now.$filename;
				$url = $rel_url.'/'.$now.$filename;
				$success = move_uploaded_file($file['tmp_name'], $url);
			}
					// if upload was successful
			if($success) {
						// save the url of the file
				$result['urls'][] = $url;
			} else {
				$result['errors'][] = "Error uploaded $filename. Please try again.";
			}
			break;
			case 3:
					// an error occured
			$result['errors'][] = "Error uploading $filename. Please try again.";
			break;
			default:
					// an error occured
			$result['errors'][] = "System error uploading $filename. Contact webmaster.";
			break;
		}
	} elseif($file['error'] == 4) {
			// no file was selected for upload
		$result['nofiles'][] = "No file Selected";
	} else {
			// unacceptable file type
		$result['errors'][] = "$filename cannot be uploaded. Acceptable file types: gif, jpg, png.";
	}

	return $result;

}


}
